﻿package 
{
	import flash.display.*;
	import flash.utils.Timer;
	import flash.events.*;
	
	public class Player{
		
		public static var player:MovieClip;
		var state:String=new String();
		var timer:Timer=new Timer(40);
		var xd:int=0;
		var yd:int=0;
		var faceWhere:String="right";
		var ifPlay:Boolean=false;
		
		public function onTick(event:TimerEvent):void 
        {
			if(player!=null)
			{
				player.x+=xd;
				player.y+=yd;
			}
		}
		
		public function Player(){
			timer.addEventListener(TimerEvent.TIMER, onTick);
			timer.start();
		}
		
		public function walkLeft(){
			if(player!=null&&(state=="stand"||ifPlay==false))
			{
				player.play();
			}
			
			if(player!=null&&faceWhere=="right")
			{
				player.scaleX=-player.scaleX;
				player.x+=player.width/2;
				faceWhere="left";
			}

			
			xd=-4;
			state="walkLeft";
		}
		public function walkRight(){
			if(player!=null&&(state=="stand"||ifPlay==false))
			{
				player.play();
			}
			
			if(player!=null&&faceWhere=="left")
			{
				player.scaleX=-player.scaleX;
				player.x-=player.width/2;
				faceWhere="right";
			}

			xd=4;
			state="walkRight";
		}
		public function stand(){
			if(player!=null&&state!="stand")
			{
				player.gotoAndStop(4);
			}

			xd=0;
			state="stand";
		}
		public function getState():String{
			return state;
		}
	}
}